//
//  DPXQViewController.m
//  项目A
//
//  Created by lanou3g on 15/9/4.
//  Copyright (c) 2015年 王钧. All rights reserved.
//

#import "DPXQViewController.h"
#import "UIImageView+WebCache.h"
#import <CoreData/CoreData.h>
#import "EntityModel.h"
#import "CoreDataManager.h"
#import "OTCover.h"
#define NavigationBarHight 64.0f
#define ImageHight 200.0f

// sharSDK
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKExtension/SSEShareHelper.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import <ShareSDKUI/SSUIShareActionSheetStyle.h>
#import <ShareSDKUI/SSUIShareActionSheetCustomItem.h>
#import <ShareSDK/ShareSDK+Base.h>

@interface DPXQViewController ()
@property (nonatomic, strong) NSArray * arr;
@property (nonatomic, strong) UIScrollView * scroll;
@property (nonatomic, strong) UIButton * button;
// 提示框
@property (nonatomic, strong) UIAlertView * aler;
@end

@implementation DPXQViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"单品详情";
    UIBarButtonItem * left = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-fenxiang-1"] style:UIBarButtonItemStyleDone target:self action:@selector(FXAction)];
    self.navigationItem.rightBarButtonItem = left;
    self.scroll.delegate = self;
    // Do any additional setup after loading the view.
}

- (void) FXAction
{
    //创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:self.titelString
                                     images:self.urlImage
                                        url:[NSURL URLWithString:@"http://mob.com"]
                                      title:@"分享标题"
                                       type:SSDKContentTypeImage];
    NSMutableArray *activePlatforms = [NSMutableArray arrayWithArray:[ShareSDK activePlatforms]];
    //进行分享
    [ShareSDK showShareActionSheet:nil
                             items:activePlatforms
                       shareParams:shareParams
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   
                   switch (state) {
                           
                           
                       case SSDKResponseStateSuccess:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       case SSDKResponseStateFail:
                       {
                           if (platformType == SSDKPlatformTypeSMS && [error code] == 201)
                           {
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:@"失败原因可能是：1、短信应用没有设置帐号；2、设备不支持短信应用；3、短信应用在iOS 7以上才能发送带附件的短信。"
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"OK"
                                                                     otherButtonTitles:nil, nil];
                               [alert show];
                               break;
                           }
                           else if(platformType == SSDKPlatformTypeMail && [error code] == 201)
                           {
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:@"失败原因可能是：1、邮件应用没有设置帐号；2、设备不支持邮件应用；"
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"OK"
                                                                     otherButtonTitles:nil, nil];
                               [alert show];
                               break;
                           }
                           else
                           {
                               UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:[NSString stringWithFormat:@"%@",error]
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"OK"
                                                                     otherButtonTitles:nil, nil];
                               [alert show];
                               break;
                           }
                           break;
                       }
                       case SSDKResponseStateCancel:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                           break;
                       }
                       default:
                           break;
                   }
                   
                   
                   
               }];

    NSLog(@"分享");
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.scroll = [[UIScrollView alloc]init];
    self.scroll.frame = self.view.bounds;
    //
    self.scroll.bounces = YES;
    [self.view addSubview:self.scroll];
    
    
    self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width,285)];
//    self.imageView.frame =CGRectMake(0, 64,self.scroll.frame.size.width,240);
    self.imageView.backgroundColor = [UIColor whiteColor];
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.urlImage] placeholderImage:[UIImage imageNamed:@"iconfont-icontu"]];
    //
//    self.imageView.contentMode = UIViewContentModeScaleAspectFill;
//    self.imageView.autoresizesSubviews = YES;
    //
    [self.scroll addSubview:self.imageView];
    
    self.titelLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 370, self.view.bounds.size.width , 50)];
    self.titelLabel.backgroundColor = [UIColor whiteColor];
    self.titelLabel.text = self.titelString;
    self.titelLabel.textAlignment = YES;
    [self.scroll addSubview:self.titelLabel];
    
    self.descriptionLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 430, self.view.bounds.size.width - 40, 150)];
    
    self.descriptionLabel.backgroundColor = [UIColor whiteColor];

    self.descriptionLabel.text = self.descriptionString;
    //显示行数
    self.descriptionLabel.numberOfLines = 0;
    //自动换行
    self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
    // 自适应Label高度
    CGRect textFrame = self.descriptionLabel.frame;
    textFrame.size.height = [_descriptionLabel.text boundingRectWithSize:CGSizeMake(textFrame.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:[NSDictionary dictionaryWithObjectsAndKeys:_descriptionLabel.font,NSFontAttributeName, nil] context:nil].size.height;
    _descriptionLabel.frame = CGRectMake(20,self.view.bounds.origin.y + 430, self.view.bounds.size.width - 40, textFrame.size.height);
    [self.scroll addSubview: self.descriptionLabel];
    
    self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.button.frame = CGRectMake(70, self.view.bounds.origin.y + _imageView.bounds.size.height + _titelLabel.bounds.size.height + _descriptionLabel.bounds.size.height + 70 + 64, self.view.bounds.size.width - 140 , 40);
    self.button.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.7];
    [self.button setTitle:@"关注" forState:UIControlStateNormal];
    self.button.layer.cornerRadius = 20.0;
    [self.button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.scroll addSubview:self.button];
    
    self.scroll.contentSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height + self.imageView.bounds.size.height + self.titelLabel.bounds.size.height + self.descriptionLabel.bounds.size.height + self.button.bounds.size.height - 330);
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) buttonAction
{
    self.arr = [[CoreDataManager defaultManager] fetchManagedObjectModelWithName:@"EntityModel" predicate:nil sortKeys:nil];
    NSMutableArray * arr = [NSMutableArray array];
    for (EntityModel * model in self.arr) {
        [arr addObject:model.titel];
    }
            // 在数组中与titel属性进行比对
        if ([arr containsObject:self.titelString]) {
           self.aler = [[UIAlertView alloc]initWithTitle:@"提示" message:@"已经关注啦！" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [self.aler show];
            [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector: @selector(perDiss)  userInfo:nil repeats:NO];
        }else
        {
            
            NSDictionary * dic = @{@"titel":self.titelString,@"imageurl":self.urlImage,@"descriptions":self.descriptionString};
            [[CoreDataManager defaultManager] addManagedObjectModelWithName:@"EntityModel" dictionary:dic];
            self.aler = [[UIAlertView alloc]initWithTitle:@"提示" message:@"添加关注成功!"delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [self.aler show];
            [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector: @selector(perDiss)  userInfo:nil repeats:NO];
        }
    
}

- (void) perDiss
{
    [self.aler dismissWithClickedButtonIndex:0 animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
